package com.gitee.huanminabc.fastboot.orm.base;

import com.gitee.huanminabc.fastboot.orm.methods.BaseMethod;
import com.gitee.huanminabc.fastboot.orm.methods.combine.InsertOrUpdateMethod;
import com.gitee.huanminabc.fastboot.orm.methods.delete.DeleteByIdMethod;
import com.gitee.huanminabc.fastboot.orm.methods.delete.DeleteByIdsMethod;
import com.gitee.huanminabc.fastboot.orm.methods.delete.DeleteMethod;
import com.gitee.huanminabc.fastboot.orm.methods.insert.InsertBatchMethod;
import com.gitee.huanminabc.fastboot.orm.methods.insert.InsertMethod;
import com.gitee.huanminabc.fastboot.orm.methods.select.*;
import com.gitee.huanminabc.fastboot.orm.methods.update.UpdateByIdMethod;
import com.gitee.huanminabc.fastboot.orm.methods.update.UpdateMethod;
import com.gitee.huanminabc.fastboot.proxy.InterceptorHandle;

import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

/**
 * 简要描述
 *
 * @Author: huanmin
 * @Date: 2025/4/5 16:07
 * @Version: 1.0
 * @Description: 文件作用详细描述....
 */
public class BaseMapperInterceptorHandle implements InterceptorHandle {
    private  static  final Map<String, BaseMethod> baseMethodMap = new HashMap<>();
    static {
        registerMethod(new DeleteByIdMethod());
        registerMethod(new DeleteByIdsMethod());
        registerMethod(new DeleteMethod());
        registerMethod(new InsertMethod());
        registerMethod(new InsertOrUpdateMethod());
        registerMethod(new InsertBatchMethod());

        registerMethod(new UpdateByIdMethod());
        registerMethod(new UpdateMethod());

        registerMethod(new ExistsMethod());
        registerMethod(new SelectByIdMethod());
        registerMethod(new SelectByIdsMethod());
        registerMethod(new SelectCountMethod());
        registerMethod(new SelectListMethod());
        registerMethod(new SelectMapMethod());
        registerMethod(new SelectMapsMethod());
        registerMethod(new SelectMapsPageMethod());
        registerMethod(new SelectOneMethod());
        registerMethod(new SelectPageMethod());
    }

    public static void registerMethod( BaseMethod baseMethod) {
        baseMethodMap.put(baseMethod.getSignature(), baseMethod);
    }

    @Override
    public Object after(Object target, Class<?> realClass, Method method, Object[] args, Object result, Map<String,Object> context) {
        BaseMethod baseMethod = baseMethodMap.get(method.getName());
        if (baseMethod!=null){
            return  baseMethod.execute( method, args);
        }
        return InterceptorHandle.super.after(target,realClass, method, args, result,context);
    }

}
